package edu.szte.ovrt.datahandler.core;

import java.io.PrintWriter;
import java.io.Writer;

import edu.szte.ovrt.datahandler.model.*;

public abstract class AbstractDocumentWriter implements DocumentWriter {
	protected final PrintWriter m_writer;

	public AbstractDocumentWriter(final Writer writer) {
		m_writer = new PrintWriter(writer);
	}

	@Override
	public void write(final Document document) throws DocumentWriterException {
		beginDocument(document);
		for (final Section section : document.getSections()) {
			write(section);
		}
		endDocument(document);
		m_writer.close();
	}

	protected void write(final Section section) throws DocumentWriterException {
		beginSection(section);
		for (final Item item : section.getItems()) {
			write(item);
		}
		endSection(section);
	}

	abstract protected void beginDocument(Document document) throws DocumentWriterException;

	abstract protected void endDocument(Document document) throws DocumentWriterException;

	abstract protected void beginSection(Section section) throws DocumentWriterException;

	abstract protected void endSection(Section section) throws DocumentWriterException;

	abstract protected void write(Item item) throws DocumentWriterException;
}
